package array;

/**
 * @author 十八岁讨厌编程
 * @date 2025/6/27 21:01
 * @PROJECT_NAME leetcode
 * @description
 */

public class Solution209 {

    public int minSubArrayLen(int target, int[] nums) {
        int head = 0;
        int total = 0;
        int minLength = Integer.MAX_VALUE;
        for (int end = 0; end < nums.length; end++) {
            total += nums[end];
            if (total < target) {
                if (head == 0 && end == nums.length - 1) return 0;
                continue;
            }
            while (total - nums[head] >= target) total -= nums[head++];
            int currentLength = end - head + 1;
            if (currentLength < minLength) minLength = currentLength;
        }
        return minLength;
    }


}
